package it.blueocean.acanto.platform.web.user;

import it.blueocean.acanto.entities.platform.DBUser;
import it.blueocean.acanto.entities.platform.Group;
import it.blueocean.acanto.platform.dao.GroupDao;
import it.blueocean.acanto.platform.dao.UserDao;
import it.blueocean.acanto.platform.filter.SimpleFilterBean;
import it.blueocean.acanto.platform.web.ABaseAcantoFilteredController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController extends ABaseAcantoFilteredController
{
	private final static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Inject
	@Named("userDao")
	private UserDao userDao;
	
	@Inject
	@Named("groupDao")
	private GroupDao groupDao;

	private static final String SESSION_FILTER_NAME = "userFilter";
	
	@RequestMapping(method = RequestMethod.GET, value = "/user/listItem.htm")
	public String listUserView()
	{
		logger.info("invoked listItem.htm");

		return "redirect:" + "/user/list.htm";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/user/list.htm")
	public String list()
	{
		logger.info("invoked list.htm");

		return "secure/user/list";
	}
	
	@Override
	@RequestMapping(method = RequestMethod.POST, value = "/user/simpleFilter.htm")
	public String simpleFilter(@RequestParam("filterName") String filterName,
			@RequestParam("filterValue") String filterValue, 
			HttpSession session, HttpServletRequest request)
	{
		SimpleFilterBean localSimpleFilterBean = new SimpleFilterBean();
		localSimpleFilterBean.setFilterName(filterName.trim());
		localSimpleFilterBean.setFilterValue(filterValue.trim());
		
		session.setAttribute(getSessionFilterName(), localSimpleFilterBean);

		return "redirect:" + "/user/list.htm";
	}
	
	@Override
	@RequestMapping(method = RequestMethod.GET, value = "/user/resetFilter.htm")
	public String resetFilter(HttpSession session)
	{
		session.removeAttribute(getSessionFilterName());

		return "redirect:" + "/user/list.htm";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/user/newItem.htm")
	public String newItem()
	{
		logger.info("invoked new.htm");
		
		return "redirect:" + "/user/new.htm";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/user/new.htm")
	public String viewNew(ModelMap model)
	{
		logger.info("invoked viewNew.htm");
		
		DBUser user = new DBUser(); 
		model.addAttribute("newUser", user);
		model.addAttribute("associatedGroups", new ArrayList<String>());
		model.addAttribute("availGroups", getListGroupName(groupDao.retrieve()));

		return "secure/user/new";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/user/editItem.htm")
	public String editItem(@RequestParam("itemId") String itemId, ModelMap model)
	{
		logger.info("invoked new.htm");
		
		DBUser user = userDao.retrieve(itemId);
		model.addAttribute("newUser", user);
		
		model.addAttribute("associatedGroups", getListGroupName(groupDao.associatedGroups(itemId)));
		model.addAttribute("availGroups", getListGroupName(groupDao.availableGroups(itemId)));
		
		return "secure/user/update";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/user/create.htm")
	public String create(@ModelAttribute("newUser") DBUser user, ModelMap model)
	{
		logger.info("invoked create.htm");
		
		userDao.store(user);
		
		return "forward:" + "/user/update.htm";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/user/update.htm")
	public String view(@ModelAttribute("newUser") DBUser user)
	{
		logger.info("invoked update.htm");

		return "secure/user/update";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/user/delete.htm")
	public String delete(@RequestParam("deleteIds") String[] deleteIds, ModelMap model)
	{
		logger.info("invoked delete.htm");
		
		for (int i = 0; i < deleteIds.length; i++)
		{			
			DBUser dbUser = userDao.retrieve(deleteIds[i]);
			userDao.delete(dbUser);
		}
		
		return "redirect:" + "/user/list.htm";
	}
	
	@Override
	public String getSessionFilterName()
	{
		return SESSION_FILTER_NAME;
	}
	
	private List<String> getListGroupName(List<Group> groups)
	{
		List<String> groupsName = new ArrayList<String>();

		for (Iterator<Group> iterator = groups.iterator(); iterator.hasNext();)
		{
			Group group = iterator.next();
			groupsName.add(group.getId());
		}
		
		return groupsName;
	}

}
